package besta.moon.sql.expression.function.string;

import besta.moon.sql.parser.SQLTokenizer;
import besta.moon.sql.expression.function.FunctionReturnP1StringAndBinary;

public class FunctionRTrim extends FunctionReturnP1StringAndBinary {

    @Override
    public final int getFunction() {
        return SQLTokenizer.RTRIM;
    }

    @Override
    public final boolean isNull() throws Exception {
        return param1.isNull();
    }

    @Override
    public final byte[] getBytes() throws Exception {
        if (isNull()) {
            return null;
        }
        byte[] bytes = param1.getBytes();
        int length = bytes.length;
        while (length > 0 && bytes[length - 1] == 0) {
            length--;
        }
        byte[] b = new byte[length];
        System.arraycopy(bytes, 0, b, 0, length);
        return b;
    }

    @Override
    public final String getString() throws Exception {
        if (isNull()) {
            return null;
        }
        String str = param1.getString();
        int length = str.length();
        while (length > 0 && str.charAt(length - 1) == ' ') {
            length--;
        }
        return str.substring(0, length);
    }

}
